#include "ParamSetFuncs.h"
#include "CfgParams.h"
#include "AntelCfgOID.h"
#include "LMX2541.h"
#include "CMX998.h"
#include "CMX7164.h"
#include "mac/mac.h"
#include "llc\llc.h"
#include "printf-stdarg.h"

unsigned char test_mode_param;
/*
int SET_UI_ASYNC_BAUD(int val)
{
	USART1_OPTIONS.baudrate = UI_ASYNC_BAUD.val_mat_int[val];
	usart_set_parameters();
	return 1;
}
int SET_UI_ASYNC_PARITY(int val)
{
	switch(val)
	{
	case 0:
		USART1_OPTIONS.paritytype = USART_NO_PARITY;
		break;
	case 1:
		USART1_OPTIONS.paritytype = USART_ODD_PARITY;
		break;
	case 2:
		USART1_OPTIONS.paritytype = USART_EVEN_PARITY;
		break;
	default:
		return -1;
	}
	usart_set_parameters();
	return 1;
}
int SET_UI_ASYNC_DATA(int val)
{
	USART1_OPTIONS.charlength = UI_ASYNC_DATA.val_mat_int[val];
	usart_set_parameters();
	return 1;
}
int SET_UI_ASYNC_STOP(int val)
{
	if(!strcmp((char *)((char *)(UI_ASYNC_STOP.val_mat_str) + val * 4),"1"))
		USART1_OPTIONS.stopbits     = USART_1_STOPBIT;
	else if(!strcmp((char *)((char *)(UI_ASYNC_STOP.val_mat_str) + val * 4),"1.5"))
		USART1_OPTIONS.stopbits     = USART_1_5_STOPBITS;
	else if(!strcmp((char *)((char *)(UI_ASYNC_STOP.val_mat_str) + val * 4),"2"))
		USART1_OPTIONS.stopbits     = USART_2_STOPBITS;
	else
	{
		USART1_OPTIONS.stopbits     = USART_1_STOPBIT;
		return -1;
	}
	usart_set_parameters();
	return 1;
}
int SET_UI_ASYNC_FLOW(int val)
{
	return 1;
}
*/
int SET_UI_SPI_BAUD(int val)
{
	return 1;
}
int SET_UI_SPI_DATA(int val)
{
	return 1;
}

int SET_UI_UDEF(int val)
{
	return 1;
}

int SET_UI_MODE_PORT(int val)
{
	return 1;
}

int SET_WL_LINK_CH(unsigned int val) //unsigned char
{
	unsigned char Old_sym_rate = 0;
	ConfigRoot.G_Wireless.SG_Uhf.param_carrier_freq_number = 0;
	if(CfgFileExist)
	{
		if(mac_state == MAC_TEST)
		{
			mac_state = MAC_TEST_NO_MOD ;
			aux_dac_0_control.Word_val = 0xE000;
			CMX7164_Write_WORD((CMX_WORD*)&aux_dac_0_control);
			CMX7164_RX_Enable(0);
			CMX7164_Tx_Enable(0);
			CMX7164_IDLE();
		}
		Old_sym_rate = Current_Channel.sym_rate;
		USER_OID_CHANNEL(&Current_Channel, val);
//		USER_OID_OUTPUT_POWER_WL(&(ConfigRoot.WirelessLink.WirelessLink.Pow.value));
		if(LMX2541_Set_Chanel_Freq() < 0)
			return 0;
		if(Current_Channel.pow < ConfigRoot.G_Wireless.SG_Uhf.param_output_pow)
			ConfigRoot.G_Wireless.SG_Uhf.param_output_pow = Current_Channel.pow ;
		if(CMX998_Set_Output_Power_dBm(ConfigRoot.G_Wireless.SG_Uhf.param_output_pow/2, CMX_SET_WITH_NO_ERR) < 0)
			return 0;
		if(Current_Channel.sym_rate != Old_sym_rate)
			if(CMX7164_Set_Cannel_Spec(Current_Channel.sym_rate) < 0)
				return 0;
		CMX7164_Reinitialize();
	}
	/// Current Channel should be passed to CMX7164 Driver - ParamSets
	return 1;
}
int SET_WL_LINK_PWRB(int val) //char
{
	double value = 0;
	if(Current_Channel.pow < val)
		return 0;
	else
		value = (double)val;
	if(CMX998_Set_Output_Power_dBm(value/2,CMX_SET_WITH_ERR) > 0)
		return 1;
	/// Power should be passed to CMX7164 Driver - ParamSets
	return 0;
}
int SET_WL_LINK_PWRL(int val) //char
{
	if(CMX998_Set_Output_Power_lvl(val) > 0)
		return 1;
	return 0;
}
int SET_WL_LINK_MOD(unsigned int val) //unsigned char
{
	int i = CMX7164_Set_modulation_type_command(val);
	CMX7164_Reinitialize();
	return i;
}
int SET_WL_LINK_FEC(unsigned int val) //unsigned char
{
	if(CMX7164_Modulation_Type >= 1 && CMX7164_Modulation_Type <= 3)
	{
		if(val == 0 || val == 1 || val == 15)
			return 0;
	}
	else if(CMX7164_Modulation_Type >= 4 && CMX7164_Modulation_Type <= 6)
	{
		if(val >= 2 && val <= 5)
			return 0;
	}
	return 1;
}
int SET_WL_LINK_PROTOCOL(unsigned int val) //unsigned char
{
	CMX7164_Reinitialize();
	return 1;
}
int SET_WL_LINK_SCRAM(unsigned int val) //unsigned char
{
	return 1;
}
int SET_WL_LINK_SPACE(unsigned int val)
{
	if(CMX7164_Boud_Rate_Operation(val))
		return 0;
	CMX7164_Reinitialize();
	return 1;
}
int SET_WL_LINK_BAUD(unsigned int val)
{
	if(CMX7164_Boud_Rate_Operation(val))
		return 0;
	CMX7164_Reinitialize();
	return 1;
}

int SET_ADV_DPORT_PRIME(int val)
{
	return 1;
}
int SET_WL_DPORT_INTERFACE_TIMEOUT(unsigned int val) //unsigned char
{
	return 1;
}
int SET_ADV_DPORT_ECHO(int val)
{
	return 1;
}
int SET_ADV_DPORT_RATE(int val)
{
	USART1_OPTIONS.baudrate = val;
	usart_set_parameters();
	return 1;
}
int SET_ADV_DPORT_BITS(int val)
{
	USART1_OPTIONS.charlength = val;
	usart_set_parameters();
	return 1;
}
int SET_ADV_DPORT_PARITY(int val)
{
	switch(val)
	{
	case 1:
		USART1_OPTIONS.paritytype = USART_NO_PARITY;
		break;
	case 3:
		USART1_OPTIONS.paritytype = USART_ODD_PARITY;
		break;
	case 2:
		USART1_OPTIONS.paritytype = USART_EVEN_PARITY;
		break;
	default:
		return 0;
	}
	usart_set_parameters();
	return 1;
}
int SET_ADV_DPORT_FLOW(int val)
{
	return 1;
}
int SET_ADV_DPORT_STOP(int val)
{
	switch(val)
	{
	case 2:
		USART1_OPTIONS.stopbits     = USART_1_STOPBIT;
		break;
	case 3:
		USART1_OPTIONS.stopbits     = USART_1_5_STOPBITS;
		break;
	case 4:
		USART1_OPTIONS.stopbits     = USART_2_STOPBITS;
		break;
	default:
		return 0;
	}
	usart_set_parameters();
	return 1;
}

int SET_ADV_XMOD_IMAGE(int val)
{
	return 1;
}
int SET_ADV_XMOD_CFGDN(int val)
{
	return 1;
}
int SET_ADV_XMOD_CFGUP(int val)
{
	return 1;
}

int SET_ADV_SIM_PRIOR(int val)
{
	if(val < 0 || val > 7)
		return 0;
	llc_sim_rim_init.llc_sim_rim_priority = val;
	return 1;
}
int SET_ADV_SIM_PROB(int val)
{
	if(val < 0 || val > 7)
		return 0;
	llc_sim_rim_init.llc_sim_probability = val;
	return 1;
}
int SET_ADV_SIM_WTIME(int val)
{
	if(val < 0 || val > 255)
		return 0;
	llc_sim_rim_init.llc_sim_waiting_time = val;
	return 1;
}

int SET_ADV_TEST_TSTSGL(int val)
{
	switch(val)
	{
	case 0: //'\0'
		mac_state = MAC_TEST;
		test_mode_param = MAC_TEST_NO_MOD;
		vTaskResume(mac_task_handle);
		printk("Continuous Wave Transmit Mode\n\rTo exit this mode please reboot\r\n");
		break;
	case 1: //-CW
		mac_state = MAC_TEST;
		test_mode_param = MAC_TEST_NONE_MOD_SIG;
		vTaskResume(mac_task_handle);
		printk("Continuous Wave Transmit Mode\n\rTo exit this mode please reboot\r\n");
		break;
	case 2: //-Continuous bit streams of all 1-s (4QAM modulated);
		mac_state = MAC_TEST;
		test_mode_param = MAC_TEST_NONE_MOD_SIG;
		vTaskResume(mac_task_handle);
		printk("Continuous Wave Transmit Mode\n\rTo exit this mode please reboot\r\n");
		break;
	case 3: //-R
		mac_state = MAC_TEST;
		test_mode_param = MAC_TEST_PRBS;
		vTaskResume(mac_task_handle);
		printk("Random Data Transmit Mode\n\rTo exit this mode please reboot\r\n");
		break;
	case 4: //Continuous Double Sideband signal (GMSK modulated);
		mac_state = MAC_TEST;
		test_mode_param = MAC_TEST_TRANSMIT_DATA;
		vTaskResume(mac_task_handle);
		printk("Random Data Transmit Mode\n\rTo exit this mode please reboot");
		break;
	case 5: //-P
		test_mode_param = MAC_TEST_PREAMBLE;
		mac_state = MAC_TEST;
		vTaskResume(mac_task_handle);
		printk("Test Mode Preamble Transmit\n\rTo exit this mode please reboot\r\n");
		break;
	case 6: //D
		mac_state = MAC_TEST;
		test_mode_param = MAC_TEST_TRANSMIT_DATA;
		vTaskResume(mac_task_handle);
		printk("Random Data Transmit Mode\n\rTo exit this mode please reboot");
		break;
	}
	return 1;
}
int SET_ADV_TEST_SETFRQ(int val)
{
	if(CfgFileExist)
	{
		if(mac_state == MAC_TEST)
		{
			mac_state = MAC_TEST_NO_MOD ;
			aux_dac_0_control.Word_val = 0xE000;
			CMX7164_Write_WORD((CMX_WORD*)&aux_dac_0_control);
			CMX7164_RX_Enable(0);
			CMX7164_Tx_Enable(0);
			CMX7164_IDLE();
		}
		unsigned int OldFreq = Current_Channel.freq;
		Current_Channel.freq = val;
		LMX2541_Set_Chanel_Freq();
		CMX998_Set_Output_Power_dBm(Current_Channel.pow/2 , CMX_SET_WITH_NO_ERR);
		Current_Channel.freq = OldFreq;
		CMX7164_Reinitialize();
	}
	/// Current Channel should be passed to CMX7164 Driver - ParamSets
	return 1;
}
/*int SET_NET_DL_MOD(int val)
{
	CMX7164_Set_modulation_type(val);
	return 1;
}
int SET_WL_LINK_FHPN(int val)
{
	// Not implemented
	return -1;
}
int SET_WL_WIFI_TYPE(int val)
{
	return 1;
}
int SET_WL_WIFI_WEP(char* val)
{
	return 1;
}
int SET_WL_WIFI_WPA(char* val)
{
	return 1;
}

int SET_WL_BT(int val)
{
	return 1;
}
*/
int SET_NET_NETPR_BRREG(unsigned int val)
{
	return 1;
}
int SET_NET_NETPR_SNMPREG(unsigned int val)
{
	return 1;
}
int SET_NET_NETPR_TIMEOUT(unsigned int val)
{
	return 1;
}
int SET_NET_WIFI_NETREG(unsigned int val)
{
	return 1;
}
int SET_NET_WIFI_ACCPOINTNAME(unsigned int val)
{
	return 1;
}
int SET_NET_WIFI_SSID(unsigned int val)
{
	return 1;
}


int SET_NET_TCPIP_REG(unsigned int val)
{
	return 1;
}
int SET_NET_TCPIP_IPADDRESS(unsigned int val)
{
	struct ip_addr ipaddress;
	ipaddress.addr = val;
	llc_netif->ip_addr = ipaddress;
	return 1;
}
int SET_NET_TCPIP_SUBNETMASK(unsigned int val)
{
	struct ip_addr ip_mask;
	ip_mask.addr = val;
	llc_netif->netmask = ip_mask;
	return 1;
}
int SET_NET_TCPIP_DEFAULTGATEWAY(unsigned int val)
{
	struct ip_addr ip_gate;
	ip_gate.addr = val;
	llc_netif->gw = ip_gate;
	return 1;
}
int SET_NET_TCPIP_DNSSERVER(unsigned int val)
{
	struct ip_addr ip_dns;
	ip_dns.addr = val;
	//llc_netif-> = ip_gate; //the dns variable must be add into netif.h
	return 1;
}
int SET_NET_SECURITY_SECURITYTYPE(unsigned int val)
{
	return 1;
}
int SET_NET_SECURITY_ENCRYPTIONTYPE(unsigned int val)
{
	return 1;
}
int SET_NET_SECURITY_SECURITYKEY(unsigned int val)
{
	return 1;
}


/*
int SET_NET_APP_HTTPD(int val)
{
	return 1;
}
int SET_NET_APP_TRANS(int val)
{
	return 1;
}

int SET_NET_DL_MNG(int val)
{
	return 1;
}
int SET_NET_DL_DIST(int val)
{
	return 1;
}
*/
/*
int SET_NET_DL_FEC(int val)
{
	return 1;
}*/
/*
int SET_NET_MCAST_IP(unsigned int val)
{
	return 1;
}
int SET_NET_MCAST_MASK(unsigned int val)
{
	return 1;
}
int SET_NET_MCAST_PORT(unsigned int val)
{
	return 1;
}
*/
/*
int SET_STATE_STS_SYNC(unsigned int val)
{
	return 1;
}
int SET_STATE_STS_RSSI(int val)
{
	return 1;
}
int SET_STATE_STS_SNR(int val)
{
	return 1;
}
int SET_STATE_STS_EVM(int val)
{
	return 1;
}
int SET_STATE_STS_BER(int val)
{
	return 1;
}
int SET_STATE_STS_FRQERR(int val)
{
	return 1;
}
int SET_STATE_STS_UCHAN(int val)
{
	return 1;
}
int SET_STATE_STAT_TXCNT(unsigned int val)
{
	return 1;
}
int SET_STATE_STAT_RXCNT(unsigned int val)
{
	return 1;
}
int SET_STATE_STAT_ERRCNT(unsigned int val)
{
	return 1;
}
int SET_STATE_SYSSTS_PWRIN(int val)
{
	return 1;
}
int SET_STATE_SYSSTS_PGOOD(unsigned int val)
{
	return 1;
}
int SET_STATE_SYSSTS_TEMP(int val)
{
	return 1;
}
*/


int SET_OPS_ID_USER(char* val)
{
	return 1;
}
int SET_OPS_ID_UNIT(char* val)
{
	return 1;
}
int SET_OPS_ID_LOC(char* val)
{
	return 1;
}

int SET_OPS_PWRS_MODE(int val)
{
	return 1;
}
/*
int SET_OPS_PWRS_TIMER(int val)
{
	return 1;
}
int SET_OPS_PWRS_UART(int val)
{
	return 1;
}

int SET_OPS_PWRS_USB(int val)
{
	return 1;
}
int SET_OPS_PWRS_BT(int val)
{
	return 1;
}
int SET_OPS_PWRS_WIFI(int val)
{
	return 1;
}
int SET_OPS_PWRS_UHF(int val)
{
	return 1;
}
int SET_OPS_PWRS_PIN(int val)
{
	return 1;
}
int SET_OPS_PWRS_LVL(int val)
{
	return 1;
}
*/
int SET_OPS_PWRS_QUIET(int val)
{
	return 1;
}
int SET_OPS_PWRS_RTC(int val)
{
	return 1;
}

int SET_Current_Channel_FREQ(unsigned int val)
{
	if(val == 0)
	{
		SET_WL_LINK_CH(ConfigRoot.G_Wireless.SG_Uhf.param_channel_num);
		return 1;
	}
	if(CfgFileExist)
	{
		val = RATED_FREQUENCY + val * 6250;
		if(mac_state == MAC_TEST)
		{
			mac_state = MAC_TEST_NO_MOD ;
			aux_dac_0_control.Word_val = 0xE000;
			CMX7164_Write_WORD((CMX_WORD*)&aux_dac_0_control);
			CMX7164_RX_Enable(0);
			CMX7164_Tx_Enable(0);
			CMX7164_IDLE();
		}
		unsigned int OldFreq = Current_Channel.freq;
		Current_Channel.freq = val;
		if(LMX2541_Set_Chanel_Freq() < 0)
			return 0;
		CMX998_Set_Output_Power_dBm(Current_Channel.pow/2 , CMX_SET_WITH_NO_ERR);
		Current_Channel.freq = OldFreq;
		CMX7164_Reinitialize();
	}
	/// Current Channel should be passed to CMX7164 Driver - ParamSets
	return 1;
}

